<template>
  <div class="container">
    <el-input
      style="width: 200px"
      placeholder="请输入商品名称"
      v-model="search.goods_name"
      clearable
    >
    </el-input
    ><el-input
      style="width: 200px"
      placeholder="请输入分类名称"
      v-model="search.class_name"
      clearable
    >
    </el-input>
    <el-button
      size="small"
      type="primary"
      icon="el-icon-search"
      class="handleSearch"
      @click="handleSearch()"
      >查询</el-button
    >
    <el-button
      size="small"
      type="danger"
      class="handleSearch"
      icon="el-icon-delete"
      @click="empty()"
      >清空</el-button
    >
    <el-button type="info" size="small" icon="el-icon-tickets" @click="derive()"
      >导出
    </el-button>
    <div>
      <el-button
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addSuperCheckGoods"
        >添加预盘点单商品</el-button
      >
      <el-button
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addPermission(1)"
        >添加预盘点单按照库存商品</el-button
      >
      <el-button
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addPermission(2)"
        >添加预盘点单按照库存商品超市品类</el-button
      >
      <el-button
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addPermission(3)"
        >添加预盘点单按照库存商品粮油品类</el-button
      >
      <el-button
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addPermission(4)"
        >添加预盘点单按照库存商品面食品类</el-button
      >
      <el-button
        size="small"
        type="danger"
        icon="el-icon-delete"
        @click="handleDeleteList()"
        >删除</el-button
      >
      <el-button
        size="small"
        type="danger"
        icon="el-icon-delete"
        @click="handleDeleteAll()"
        >删除全部</el-button
      >
    </div>

    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_code"
        label="商品Code"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_bar_code"
        label="条形码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_name"
        label="分类"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="spec"
        label="规格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rk_spec"
        label="入库规格"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <SuperCheckGoodsDialog
      :addDialog="addDialog"
      @tag="getData"
    ></SuperCheckGoodsDialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/pagination";
import {
  getReadyStockGoodList,
  addReadyStockGoodByStockGood,
  deleteReadyStockGood,
} from "@/api/check/superCheck";
import SuperCheckGoodsDialog from "@/components/check/superCheckGoodsDialog";
export default {
  components: {
    pagination: Pagination,
    SuperCheckGoodsDialog: SuperCheckGoodsDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      addDialog: {
        centerDialogVisible: false,
      },
      search: {
        goods_name: "",
        class_name: "",
      },
      multipleSelection: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        erp_goods_codes: "",
        erp_goods_bar_codes: "",
        class_codes: "",
        goods_name: this.search.goods_name,
        class_name: this.search.class_name,
      };

      getReadyStockGoodList(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Takestock/exportReadyStockGoodList?token=" +
            getToken() +
            "&erp_goods_codes=" +
            "&erp_goods_bar_codes=" +
            "&class_codes=" +
            "&goods_name=" +
            this.search.goods_name +
            "&class_name=" +
            this.search.class_name;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    // 清空
    empty() {
      this.search = {
        goods_name: "",
        class_name: "",
      };
    },
    //添加预盘点单商品
    addSuperCheckGoods() {
      this.addDialog.centerDialogVisible = true;
    },
    //添加预盘点单按照库存商品
    addPermission(num) {
      switch (num) {
        case 1:
          this.$confirm("是否添加预盘点单按照库存商品?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              addReadyStockGoodByStockGood().then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg || res.message,
                  });
                  this.getData();
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg || res.message,
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消添加",
              });
            });
          break;
        case 2:
          this.$confirm("是否添加预盘点单按照库存商品超市品类?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              let data = {
                type: "chaoshi",
              };
              addReadyStockGoodByStockGood(data).then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg || res.message,
                  });
                  this.getData();
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg || res.message,
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消添加",
              });
            });
          break;
        case 3:
          this.$confirm("是否添加预盘点单按照库存商品粮油品类?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              let data = {
                type: "liangyou",
              };
              addReadyStockGoodByStockGood(data).then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg || res.message,
                  });
                  this.getData();
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg || res.message,
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消添加",
              });
            });
          break;
          case 4:
          this.$confirm("是否添加预盘点单按照库存商品面试品类?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              let data = {
                type: "mianshi",
              };
              addReadyStockGoodByStockGood(data).then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: res.msg || res.message,
                  });
                  this.getData();
                } else {
                  this.$message({
                    type: "info",
                    message: res.msg || res.message,
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消添加",
              });
            });
          break;
      }
    },
    //删除
    handleDelete(scope) {
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            ids: scope.row.id,
          };
          deleteReadyStockGood(data).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
              this.getData();
            } else {
              this.$message({
                type: "info",
                message: res.msg || res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //删除列表勾选
    handleDeleteList(scope) {
      this.$confirm("是否删除该信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.multipleSelection.length == 0) {
            this.$message({
              type: "info",
              message: "请选择商品",
            });
            return false;
          } else {
            let data = {
              ids: this.multipleSelection.join(","),
            };
            deleteReadyStockGood(data).then((res) => {
              if (res.code == 0) {
                this.$message({
                  type: "success",
                  message: res.msg || res.message,
                });
                this.getData();
              } else {
                this.$message({
                  type: "info",
                  message: res.msg || res.message,
                });
              }
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //删除全部
    handleDeleteAll(scope) {
      this.$confirm("是否删除全部?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            ids: "all",
          };
          deleteReadyStockGood(data).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
              this.getData();
            } else {
              this.$message({
                type: "info",
                message: res.msg || res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = [];
      let valMain = val;
      valMain.forEach((res) => {
        this.multipleSelection.push(res.id);
      });
    },
  },
};
</script>
<style scoped>
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>